package com.portomx.agenda.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.portomx.agenda.bo.DireccionBo;
import com.portomx.agenda.bo.PersonaBo;
import com.portomx.agenda.factory.DireccionFactory;
import com.portomx.agenda.factory.PersonaFactory;
import com.portomx.agenda.model.Direccion;
import com.portomx.agenda.model.Persona;

public class PersonaController extends HttpServlet {

	private static final int ALTA_PERSONA = 1;
	private static final int GET_ALL = 2;
	private static final int CONSULTA_ID = 3;
	private static final int UPDATE = 4;

	private PersonaBo personaBo;
	private DireccionBo direccionBo;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		WebApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(this.getServletContext());
		personaBo = context.getBean(PersonaBo.class);
		direccionBo = context.getBean(DireccionBo.class);
		
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = null;
		int method = Integer.parseInt(request.getParameter("method"));
		switch (method) {
		case ALTA_PERSONA:
			dispatcher = getServletContext().getRequestDispatcher(
					"/vistas/Welcome.jsp");
			Persona persona = PersonaFactory.newInstance();
			Direccion direccion = DireccionFactory.newInstance();
			Collection<Persona> personas = new ArrayList<Persona>();
			persona.setNombre(request.getParameter("txtNombre"));
			persona.setEmail(request.getParameter("txtmail"));
			Long tel = Long.parseLong(request.getParameter("txtTelefono"));
			persona.setTelefono(tel);
			personaBo.create(persona);
			direccion.setCalle(request.getParameter("txtCalle"));
			Integer cp = Integer.parseInt(request.getParameter("txtCP"));
			direccion.setCodigoPostal(cp);
			direccion.setColonia(request.getParameter("txtColonia"));
			Integer num = Integer.parseInt(request.getParameter("txtNo"));
			direccion.setNumero(num);
			personas.add(persona);
			personas.add(personaBo.read(2L));
			direccion.setPersonas(personas);
			direccionBo.create(direccion);
			dispatcher.forward(request, response);
			break;

		case GET_ALL:
			dispatcher = getServletContext().getRequestDispatcher(
					"/vistas/ListadoPersonas.jsp");
			request.setAttribute("personas", personaBo.getAll());
			dispatcher.forward(request, response);
			break;
		case CONSULTA_ID:
			dispatcher = getServletContext().getRequestDispatcher(
					"/vistas/Editar.jsp");
			Long id = Long.parseLong(request.getParameter("persona"));
			request.setAttribute("person", personaBo.read(id));
			dispatcher.forward(request, response);
			break;

		case UPDATE:
			dispatcher = getServletContext().getRequestDispatcher("/vistas/Welcome.jsp");
			personaBo.update(crearUpdate(request));
			dispatcher.forward(request, response);
			break;
		default:
			break;
		}
	}
	
	private Persona crearUpdate(HttpServletRequest request){
		Persona persona = PersonaFactory.newInstance();
		persona.setId(Long.parseLong(request.getParameter("txtid")));
		persona.setEmail(request.getParameter("txtemail"));
		persona.setNombre(request.getParameter("txtnombre"));
		persona.setTelefono(Long.parseLong(request.getParameter("txttelefono")));
		return persona;
	}
}
